package com.ljy.my_study.JDK动态代理;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/** 
* @author James
* @date 2018年6月6日 
*/
public class RealSubject implements Subject{

	@Override
	public void doSomething() {
		System.out.println("RealSubject call doSomething();");
	}

	static class ProxyHandler implements InvocationHandler{
		private Object tar;
		
		public Object getTar() {
			return tar;
		}

		public void setTar(Object tar) {
			this.tar = tar;
		}

		public Object bind(Object tar) {
			setTar(tar);
			return Proxy.newProxyInstance(tar.getClass().getClassLoader(), tar.getClass().getInterfaces(), this);
		}
		
		@Override
		public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
			System.out.println(tar.getClass().getSimpleName()+" ProxyHandler call "+method.getName()+"();");
			return null;
		}
		
	}
	
	public static void main(String[] args) {
		Subject subject=(Subject)new ProxyHandler().bind(new RealSubject());
		subject.doSomething();
	}
}

